/*******************************************************************************
 * ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of CCValidator.
 * Copyright 2009 Corlatti (http://www.corlatti.com.ar/)
 *
 * CCValidator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * CCValidator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ***** END LICENSE BLOCK *****
 *
 * $Id: CCValidationRules.js 16 2009-03-29 07:59:32Z corlatti $
 * 
 * This file contains core validations rules.
 * 
 *******************************************************************************/
/* validation rules, here you can add your own rules */
function CCValidationRule(){
}
CCValidationRule.prototype._validate = function(objName, value){
	return new Array(false, 'no rules defined');
}
CCValidationRule.prototype._addMsg = function(objName, msg){
	return msg;
}

/* not empty, required field */
Required.prototype = new CCValidationRule();
function Required(objName, value, rule){
	var resultado = true;
	var msg = '';
	if(value.trim() == ''){
		resultado = false;
		msg = this._addMsg(objName, 'Es un campo requerido');
	}
	return new Array(resultado, msg);
}

/* number 
 * accepts numbers, that means positives, negatives, integers, decimals etc.
 * */
Numeric.prototype = new CCValidationRule();
function Numeric(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(isNaN(parseInt(value.trim()))){
			resultado = false;
			msg = this._addMsg(objName, 'Debe ser un valor numerico.');
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* integer 
 * only accepts valid numbers without decimals
 * uses Numeric validation
 * */
Integer.prototype = new CCValidationRule();
function Integer(objName, value, rule){
	var resultado = false;
	var msg = '';
	try {
		var numeric = new Numeric(objName, value, rule);
		if(numeric){
			var regExp  = /(^-?\d\d*$)/;
			if(regExp.test(value)){
				resultado = true;
			}
		}
		if(!resultado){
			msg = this._addMsg(objName, 'Debe ser un valor entero.');
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* decimal 
 * accepts valid numbers with or without decimals
 * uses Numeric validation
 * Miles separator ,
 * Decimal separator .
 * */
Decimal.prototype = new CCValidationRule();
function Decimal(objName, value, rule){
	var resultado = false;
	var msg = '';
	try {
		var numeric = new Numeric(objName, value, rule);
		if(numeric){
			var regExp  = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
			if(regExp.test(value)){
				resultado = true;
			}
		}
		if(!resultado){
			msg = this._addMsg(objName, 'Debe ser un valor entero o con decimales.');
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* only letters and spaces */
Alpha.prototype = new CCValidationRule();
function Alpha(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(value != ''){
			var reg = /^[a-zA-Z ]+$/;
			if(!value.match(reg)){
				resultado = false;
				msg = this._addMsg(objName, 'Debe contener solo letras.');
			}
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* letters, numbers and spaces */
Alphanumeric.prototype = new CCValidationRule();
function Alphanumeric(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(value != ''){
			var reg = /^[a-zA-Z0-9 ]+$/;
			if(!value.match(reg)){
				resultado = false;
				msg = this._addMsg(objName, 'Debe contener solo letras y/o numeros.');
			}
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* minimal value */
Min.prototype = new CCValidationRule();
function Min(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		var tmp = rule.split('=');
		var minValue = tmp[1];
		if(minValue > value){
			resultado = false;
			msg = this._addMsg(objName, 'Debe ser mayor a ' + minValue);
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* max value */
Max.prototype = new CCValidationRule();
function Max(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		var tmp = rule.split('=');
		var maxValue = tmp[1];
		if(maxValue > value){
			resultado = false;
			msg = this._addMsg(objName, 'Debe ser menor a ' + maxValue);
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* mail validation */
Mail.prototype = new CCValidationRule();
function Mail(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(value != '' && value != null){
			var objRegExp  = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;
			if(!objRegExp.test(value)){
				resultado = false;
				msg = this._addMsg(objName, 'Debe contener una direccion de e-mail valida.');
			}
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* IP validation */
Ip.prototype = new CCValidationRule();
function Ip(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(value != '' && value != null){
			var objRegExp  = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			if(!objRegExp.test(value)){
				resultado = false;
				msg = this._addMsg(objName, 'Debe contener un IP valido.');
			}
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}

/* URL validation */
Url.prototype = new CCValidationRule();
function Url(objName, value, rule){
	var resultado = true;
	var msg = '';
	try {
		if(value != '' && value != null){
			var objRegExp  = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:~\+#]*[\w\-\@?^=%&~\+#])?/;
			if(!objRegExp.test(value)){
				resultado = false;
				msg = this._addMsg(objName, 'Debe contener una URL valida, se acepta http, https y ftp.');
			}
		}
	} catch(e){
		throw e;
	}
	return new Array(resultado, msg);
}




